<?php

/**
 * Editing a Company
 * @package ModuleGestionContacts
 * @version 1.0b
 * @var $_POST['id'] The company's ID
 */

session_start();

// Configuration file
include '../configuration.php';
// Functions
include '../inc/functions.php';

// Checking if user is logged in
if (!CheckSession()):
	exit();
endif;

// Include all objects
// TODO: Include only needed classes
foreach (glob($configuration['webroot'].'objects/*.php') as $class) { include_once $class; }

try {
	// Retrieving company
	$company = new Company();
	$company->Get($_POST['id']);
	
	$company->name = $_POST['name'];
	$company->activitysectorId = $_POST['activitysector'];
	$company->contactId = $_POST['contact'];
	
	$address = $company->GetAddressList(Array(Array('type', '=', 'MAINADDRESS')));
	$address = $address[0];
	$address->name = $_POST['ad_name'];
	$address->building = $_POST['ad_building'];
	$address->city = $_POST['ad_city'];
	$address->number = $_POST['ad_number'];
	$address->postalcode = $_POST['ad_postalcode'];
	$address->street = $_POST['ad_street'];
	$address->type = 'MAINADDRESS';
	
	$list = Array($address);
	$company->SetAddressList($list);
	
	// Updating company into database
	$address->Save();
	$company->Save();
	
	echo 'OK';

} catch (Exception $e) {
	echo $e->getMessage();
}
?>